package com.framework.web.bus.controller;


import com.framework.core.domain.AjaxJson;
import com.framework.core.easyui.domain.DataGridModel;
import com.framework.core.easyui.util.PageUtil;
import com.framework.core.util.MyBeanUtils;
import com.framework.web.bus.domain.DeliveryArea;
import com.framework.web.bus.service.DeliveryAreaService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
* @Title: Controller
* @Description: 控制层
* @author zqtao
* @date 2016-09-29
* @version V1.0
*
*/
@Controller
@RequestMapping("/deliveryArea")
public class DeliveryAreaController {

private Logger logger = LoggerFactory.getLogger(this.getClass());


/**
* Spring注入资源信息模块service
*/
@Autowired
private DeliveryAreaService deliveryAreaService;


/**
*
* 分页
* @param dgm
* @param deliveryArea
* @return
* @throws Exception
*/
@RequestMapping(value = "/list", method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> list(DataGridModel dgm, DeliveryArea deliveryArea, HttpServletRequest request) throws Exception {
    PageRequest pr = DataGridModel.getPageRequest(dgm);
    return PageUtil.getPageMap(deliveryAreaService.getPageList(deliveryArea, pr));
}

/**
*  请求新增或修改页面
*
* @return
* @throws Exception
*/
@RequestMapping(value = "/toSaveAndUpdate/{typeId}/{id}", method = RequestMethod.GET)
public ModelAndView toSaveAndUpdate(@PathVariable("typeId") String typeId,@PathVariable("id") String id)
throws Exception {
    ModelAndView modelAndView = new ModelAndView("bus/deliveryArea/deliveryAreaForm");
    DeliveryArea deliveryArea = new DeliveryArea();
    if (StringUtils.isNotEmpty(id) && !"0".equals(id)) {
        deliveryArea = deliveryAreaService.findById(id);
    } else {

    }
    deliveryArea.setTypeId(typeId);
    modelAndView.addObject("deliveryArea", deliveryArea);
    return modelAndView;
}


/**
* 保存信息
*
* @param deliveryArea  信息实体
* @return
* @throws Exception
*/
@RequestMapping(value = "/saveAndUpdate", method = RequestMethod.POST)
@ResponseBody
public AjaxJson saveAndUpdate(HttpServletRequest request, HttpServletResponse response, DeliveryArea deliveryArea) throws Exception {
    AjaxJson j = new AjaxJson();
    if (StringUtils.isNotEmpty(deliveryArea.getId())) {//更新
        DeliveryArea r = deliveryAreaService.findById(deliveryArea.getId());
        MyBeanUtils.copyBeanNotNull2Bean(deliveryArea, r);
        deliveryArea = r;
    }else {

    }
    deliveryAreaService.save(deliveryArea);
    j.setMsg("保存成功");
    return j;
}

/**
* 根据id字符串删除数据信息
*
* @param id
* @return
* @throws Exception
*/
@RequestMapping(value ="/delete", method = RequestMethod.POST)
@ResponseBody
public AjaxJson delete(String id) throws Exception {
    AjaxJson j = new AjaxJson();
    deliveryAreaService.delete(id);
    j.setMsg("删除成功");
    return j;
}



/**
 * 跳转到Datagrid
 * @param request
 * @param response
 * @return
 */
@RequestMapping(value = "/toDatagrid/{typeId}",method = RequestMethod.GET)
@ResponseBody
public ModelAndView toDatagrid(HttpServletRequest request,HttpServletResponse response,
                               @PathVariable("typeId") String typeId){

    ModelAndView mav = new ModelAndView("/bus/deliveryArea/deliveryAreaList");
    mav.addObject("typeId",typeId);
    return mav;
}

}